Repository navigation
Fix #15214, #13957 (misra.py: 2.5 for a macro defined in several files or in an inactive branch) - #9059
Fix #15214, #13957 (misra.py: 2.5 for a macro defined in several files or in an inactive branch)#9059g-chauvel wants to merge 3 commits into
Conversation
dmcppcheck
left a comment
There was a problem hiding this comment.
AI review
The fix works for the case in the description, and with -j2 the output is now the same on every run. However, the PR adds a false positive for misra 2.5. Merging is now by file+line instead of by macro name. When two translation units reach the same header through different path spellings, that header's macro gets two entries. One example is an absolute -I path, which is common with compile_commands.json, in one file and a relative #include "../inc/h.h" in another. If only one of those TUs uses the macro, it is reported as unused. The base version reports nothing. Rules 2.3 and 2.4 already have this problem: a typedef in the same setup gets a 2.3 and also 5.6 on the base version. With this PR, 2.5 has it too. Normalising the path (for example os.path.realpath) in is_different_location would fix all three rules.
Comments
| # | Comment | By | Where | Status |
|---|---|---|---|---|
| 1 | Macro merge by file+line gives 2.5 false positive when a header path is spelled differently | AI review | addons/misra.py:4949 |
🆕 New |
@g-chauvel, please fix, resolve or reply to the open and new comments: #1.
Testing
- Built
cppcheckfrompr/withmake -j8 cppcheck. The PR only changes Python, sotestrunnerwas not needed. PYTHONPATH=addons python3 -m pytest addons/test/misra_test.py: all 23 pass, including the newMisraMacrocases.pytest -k misra test/cli: 10 pass.- PR example (
m1.c/m2.c):m1.c:1:0 ... [misra-c2012-2.5], as the description says. Ifm3.cdefinesMand does not use it,-j2 m1.c m3.creports bothm1.c:1andm3.c:1, the same on 3 runs. - A header included through different relative spellings (
h.h,../h.h,../sub/../h.h) and used in only one TU: no 2.5 warning, which is correct, because simplecpp normalises those paths.
False positive (regression):
mkdir -p t2/inc t2/src && cd t2
printf '#define HM 3\n' > inc/h.h
printf '#include "h.h"\nint a1(void);\n' > src/a.c
printf '#include "../inc/h.h"\nint b1 = HM;\n' > src/b.c
cppcheck -q --template=simple --addon=misra --enable=style --suppress=misra-c2012-8.4 --suppress=misra-c2012-8.7 --suppress=misra-c2012-20.1 -I$PWD/inc src/a.c src/b.c
PR:
/work/scratch/t2/inc/h.h:1:0: style: misra violation (use --rule-texts=<file> to get proper output) [misra-c2012-2.5]
Base: no output. HM is used in src/b.c.
Passing one source file as an absolute path and the other as a relative one ($PWD/a.c sub/b.c, both including h.h) gives the same false positive, again only with the PR. The same setup with a typedef header already gives 2.3 and 5.6 on the base version, so the path comparison is a known weakness that this PR extends to 2.5.
Automated review of bd55888 by Claude. The pull request was built and tested locally.
| existing_macro['used'] = existing_macro['used'] or new_macro['used'] | ||
| else: | ||
| all_macro_info[key] = new_macro | ||
| add_declaration(all_macro_info, new_macro) |
There was a problem hiding this comment.
AI review
Before this PR, macros were merged by name, so how the header path was spelled did not matter. Now they are merged by file+line (is_different_location), and the file string differs when TUs reach the same header through different spellings, for example -I$PWD/inc with #include "h.h" in one file and #include "../inc/h.h" in another. The result is two entries, and the TU that does not use the macro gets a 2.5 warning:
/work/scratch/t2/inc/h.h:1:0: style: misra violation ... [misra-c2012-2.5]
The base version reports nothing here. Rules 2.3 and 2.4 already behave like this, and a typedef in the same setup also gets 5.6. Normalising the path in is_different_location (e.g. os.path.realpath(loc['file'])) would avoid the regression and fix the other rules too.
bd55888 to
263fd04
Compare
dmcppcheck
left a comment
There was a problem hiding this comment.
AI review
The PR adds a new false positive for misra 2.5. For each macro name, misra_2_5() keeps only the last #define directive of a TU, and it decides used by name. Inactive directives count too. Before this PR the CTU merge also went by name, so this did not matter. Now the merge goes by location. Suppose a TU uses a macro from config.h while a later #ifndef X / #define X fallback in another header also defines it. That TU then records only the fallback's location as used, and a second TU that includes config.h without using the macro gets a 2.5 warning on config.h. The base version reports nothing. The path-spelling problem from the earlier review is fixed.
Comments
| # | Comment | By | Where | Status |
|---|---|---|---|---|
| 1 | Macro merge by file+line gives 2.5 false positive when a header path is spelled differently | AI review | addons/misra.py:4949 |
✅ Fixed |
| 2 | 2.5 false positive: per-TU summary keeps only the last #define per name, so the macro that is really used can look unused | AI review | addons/misra.py:4949 |
🆕 New |
@g-chauvel, please fix, resolve or reply to the open and new comments: #2.
Testing
- Built
cppcheckinpr/withmake -j8 cppcheck. The PR only changes Python, sotestrunnerwas not needed. To compare with the base version I ran the same binary withbase/addons/misra.py. PYTHONPATH=addons python3 -m pytest addons/test/misra_test.py: all 26 pass.- I re-ran the repro from the earlier review (absolute
-Ipath in one TU,#include "../inc/h.h"in the other): no 2.5 warning any more.
False positive (regression):
printf '#define BUFSIZE 2\n' > config.h
printf '#ifndef BUFSIZE\n#define BUFSIZE 5\n#endif\n' > defaults.h
printf '#include "config.h"\n#include "defaults.h"\nint x = BUFSIZE;\n' > a.c
printf '#include "config.h"\nint b1(void);\n' > b.c
cppcheck -q --template=simple --addon=misra --enable=style --suppress=misra-c2012-8.4 --suppress=misra-c2012-8.7 a.c b.c
PR, with both -j1 and -j2:
config.h:1:0: style: misra violation (use --rule-texts=<file> to get proper output) [misra-c2012-2.5]
Base addon: no output. a.c uses BUFSIZE from config.h:1: the dump's macro-usage points there. But a.c's summary only has defaults.h:2, which is inactive.
Automated review of 263fd04 by Claude. The pull request was built and tested locally.
| existing_macro['used'] = existing_macro['used'] or new_macro['used'] | ||
| else: | ||
| all_macro_info[key] = new_macro | ||
| add_declaration(all_macro_info, new_macro) |
There was a problem hiding this comment.
AI review
Merging macros by location only works if each TU's summary has every definition with its own used flag. misra_2_5() stores one entry per name in unused_macro[macro_name], and the last directive wins. Inactive #ifndef fallbacks count as well. used is also decided by name. So a TU that uses config.h's macro can record only an inactive fallback in another header:
// config.h
#define BUFSIZE 2
// defaults.h
#ifndef BUFSIZE
#define BUFSIZE 5
#endif
// a.c
#include "config.h"
#include "defaults.h"
int x = BUFSIZE;
// b.c
#include "config.h"
int b1(void);
cppcheck --addon=misra --enable=style --suppress=misra-c2012-8.4 --suppress=misra-c2012-8.7 a.c b.c with this PR:
config.h:1:0: style: misra violation ... [misra-c2012-2.5]
The base version reports nothing. The macro-usage entries in the dump (file/line of the definition that was used) could set used per definition, and misra_2_5() should keep every #define directive instead of one per name.
…e same macro name)
… code that is ifeffed out)
263fd04 to
7b80e44
Compare
Follow-up to #9053 for 2.5:
misra_2_5()keeps only the last#defineof a name in each translation unit, andanalyse_ctu_info()merges the definitions of a name into the first one read.This PR keeps every
#defineand uses the sameadd_declarations()as 2.3 and 2.4, so each unused definition is reported.m1.cm2.cBefore PR: no output.
Now:
This PR also fixes #13957 (inactive
#define, R10 below) and handles paths that are not found from the current directory, for example with--relative-paths(R13 below). The tables list every change ofmisra.py, its visible effects, and what stays unchanged.Added during the review
Two false positives of the first versions of this PR are fixed, each with tests.
A header reached through two path spellings (review comment). The files are compared by real path, so the header is one location. 2.3, 2.4, 2.5, 5.6 and 5.7 report it with a spelling that does not go through a link, absolute when possible, and a suppression written with the relative or the absolute spelling matches it. This also fixes the same false positives of 2.3, 2.4, 5.6 and 5.7 on
upstream/main. 8.5 and 8.6 are fixed in a separate PR, and 5.9 still compares the file names as strings.inc/h.hsrc/a.csrc/b.cFirst version of this PR:
On
upstream/main, the same output without the 2.5 line.Now: no output.
A macro defined again by an inactive fallback (review comment). Fixed by keeping every
#define(C1):a.cmarksconfig.h:1as used, and the inactive fallback indefaults.his not checked (#13957).config.hdefaults.ha.cb.cPrevious versions of this PR:
Now: no output.
Code changes
misra_2_5(): every active#defineof a name (C7) goes into the summary, not only the last onemisra_2_5(): an#undefmarks as used every earlier#defineof its nameadd_declarations(), shared by 2.3, 2.4, 2.5, 5.6, 5.7: declarations of a name grouped by real path and line (see below)spelling_order(): choice of the reported path among the paths of one filereport_declarations(): 5.6 and 5.7 reported once the.ctu-infofiles are read (see below)real_path(),spelling_order(),is_named()and the lookup of an#includename that no file of the dump has (C7),path_found()(C8), last path kept inadd_declarations()real_path()), follow-up (the others)test_ctu_realpath_called_once_per_path,test_ctu_macro_header_name_looked_up_once,test_ctu_macro_absent_header_names_looked_up_once,test_ctu_macro_absent_header_names_many_directories)_get_active_directives(), used bymisra_2_5()instead of all the directives: the directives of the active branches, in the active files (see below)#undefbefore every#define) not found from the current directory: only the last definition per summary, as before C1 and C3 (see below)--relative-pathsOrigin: initial commit = first commit of the PR, review 1 and review 2 = the two review comments of "Added during the review" above, review = the review comment on robustness and performance, follow-up = needed by the previous one.
Rule 2.5
#defineof a name that another source file also defines or#undefs (#15214), except R13#undefed there, otherwise only the last one (R2) of the first source file read (see below)#undefof the name cancels it (see below)test_ctu_unused_declarationtest_ctu_macro_same_name_same_file_two_linestest_ctu_macro_undef_counts_as_usetest_ctu_macro_undef_before_definetest_addon_misra_2_5_same_name_two_files#defineof a name in one source file and its headers, except R13misra-test.c: lines 217, 1925, 1926)test_ctu_macro_same_name_source_and_headertest_ctu_macro_defined_again_after_undeftest_ctu_macro_undef_counts_as_usetest_ctu_macro_undef_before_definetest_ctu_macro_several_definitionstest_addon_misra_2_5_fallback_definitiontest_addon_misra_2_5_header_relative_paths_defined_againtest_addon_misra_2_5_source_and_headertest_ctu_declaration_path_spelling_reportedtest_ctu_declaration_other_case_reportedtest_ctu_declaration_real_case_reportedtest_ctu_declaration_real_case_reported_relativetest_ctu_declaration_real_case_reported_lower_case_drive_absolutetest_ctu_declaration_real_case_reported_through_junctiontest_ctu_declaration_real_case_reported_through_junction_parent_casetest_ctu_declaration_real_case_reported_junction_nametest_ctu_declaration_link_spelling_reportedtest_ctu_declaration_link_spelling_reported_linked_directorytest_ctu_declaration_relative_real_spelling_reportedtest_ctu_declaration_shortest_spelling_reportedtest_ctu_declaration_same_length_spelling_reportedtest_ctu_declaration_8dot3_spelling_reportedtest_ctu_declaration_shortest_before_real_case_reportedtest_addon_misra_2_5_header_link_spelling-jNwithout--cppcheck-build-dir(any executor)test_ctu_declaration_path_spelling_reportedtest_ctu_declaration_other_case_reportedtest_ctu_declaration_real_case_reportedtest_ctu_declaration_real_case_reported_relativetest_ctu_declaration_real_case_reported_lower_case_drive_absolutetest_ctu_declaration_real_case_reported_through_junctiontest_ctu_declaration_real_case_reported_through_junction_parent_casetest_ctu_declaration_real_case_reported_junction_nametest_ctu_declaration_link_spelling_reportedtest_ctu_declaration_link_spelling_reported_linked_directorytest_ctu_declaration_relative_real_spelling_reportedtest_ctu_declaration_shortest_spelling_reportedtest_ctu_declaration_same_length_spelling_reportedtest_ctu_declaration_8dot3_spelling_reportedtest_ctu_declaration_shortest_before_real_case_reportedtest_addon_misra_2_5_header_link_spellingtest_addon_misra_2_5_header_link_spelling-jN(R4)test_addon_misra_2_5_header_link_spellingtest_addon_misra_2_5_header_dotdot_spelling--error-exitcode, project whose only finding is a 2.5 that this PR adds (R1, R2, R8, R9, R11, R12) or removes (R10)#definecancelled by an#undefthat the dump lists before it, name used or#undefed in another source file, or defined again later in the dump, except R13 (see below)realpathdoes not resolve to the same file (see below)#define, in code that no checked configuration includes, for example under#if 0(Trac #13957)misra-test.c, line 219 no longer reported, line 217 reported instead), with exceptions and false negatives in headers (see below)test_ctu_macro_inactive_branchtest_ctu_macro_nested_in_inactive_branchtest_ctu_macro_else_nested_in_inactive_branchtest_ctu_macro_after_nested_endif_in_inactive_branchtest_ctu_macro_elif_branchtest_ctu_macro_elifdef_branchtest_ctu_macro_spaced_directivestest_ctu_macro_condition_evaluated_twicetest_ctu_macro_header_included_in_branchtest_ctu_macro_header_included_in_branch_other_filetest_ctu_macro_header_same_file_name_in_branchtest_ctu_macro_nested_header_same_file_name_in_branchtest_ctu_macro_header_same_file_name_in_branch_include_dirtest_ctu_macro_header_same_file_name_quote_and_angletest_ctu_macro_header_included_by_macrotest_ctu_macro_header_included_in_other_casetest_ctu_macro_header_included_in_branch_hard_linktest_ctu_macro_header_included_in_branch_hard_link_in_subdirectorytest_ctu_macro_header_included_through_linktest_ctu_macro_header_included_from_parent_directorytest_ctu_macro_header_same_file_name_other_casetest_ctu_macro_header_same_file_name_in_other_directory_casetest_ctu_macro_header_condition_not_evaluatedtest_ctu_macro_source_file_name_in_inactive_includetest_ctu_macro_undef_after_inactive_definetest_addon_misra_2_5_inactive_branchtest_addon_misra_2_5_header_in_inactive_branchtest_addon_misra_2_5_header_rp_shorter_than_include#undefin code that no checked configuration includestest_ctu_macro_undef_in_inactive_branchtest_addon_misra_2_5_undef_in_inactive_branch#defineunused in every checked configuration where it is active, name used in another checked one, except R13#undefof the name cancels it (R1)test_ctu_macro_used_in_other_configurationtest_addon_misra_2_5_used_in_other_configuration#undefbefore every#define) whose path in the dump is not found from the current directory (see below)test_ctu_macro_path_not_foundtest_ctu_macro_path_not_found_first_path_then_smallest_linetest_ctu_macro_path_not_found_undef_in_other_summarytest_ctu_macro_same_name_same_file_two_linestest_ctu_macro_undef_counts_as_usetest_ctu_macro_undef_before_definetest_ctu_macro_defined_again_after_undeftest_addon_misra_2_5_header_relative_pathstest_addon_misra_2_5_header_relative_paths_defined_againtest_addon_misra_2_5_header_link_spelling_relative_pathstest_addon_misra_2_5_header_relative_paths_first_path_smallest_line--error-exitcode, project whose only finding is a 2.5 with a suppression as in R5 or R6-jN(R4)test_addon_misra_2_5_header_link_spelling--enable=informationand--addon=misra(see below)unmatchedSuppression(<file>:0,nofile:0or*:0), exit code 1 with--error-exitcodeSide effects on other rules (shared
add_declarations())test_ctu_declaration_path_spellingtest_ctu_declaration_path_spelling_reportedtest_ctu_declaration_link_spellingtest_ctu_declaration_link_spelling_reportedtest_ctu_declaration_conflict_path_spellingtest_addon_misra_5_6_header_link_spelling.ctu-infofiles are read, mixed with each other and with 8.5, 8.6, 5.9typedefline included)unmatchedSuppressionfor the path that is not reported, unless the name is used through it (2.3, 2.4)unmatchedSuppressionfor the path that is not reported (R3), and for every path after a false positive of S1 (see below)test_addon_misra_unmatched_suppression_header_link_spelling--enable=informationand--addon=misra, project whose only finding of this id is a false positive of S1unmatchedSuppression(nofile:0, or*:0for<id>:*)test_addon_misra_unmatched_suppression_link_spelling--error-exitcode, project whose only output is anunmatchedSuppressionof S4 or S5test_addon_misra_unmatched_suppression_header_link_spelling--error-exitcode, project whose only finding is a false positive of S1test_addon_misra_5_6_header_link_spellingTests only
#undef,#ifndeffallback definition, symlink, path spelling): 6 pytest and 2 CLI tests#undefbefore the#define(CI run ofmisra-ctu-*-test.c)test_ctu_macro_unusedtest_ctu_macro_several_definitionstest_ctu_macro_undef_counts_as_usetest_ctu_macro_undef_before_definetest_ctu_macro_defined_again_after_undeftest_ctu_declaration_symlinktest_addon_misra_2_5_fallback_definitiontest_addon_misra_2_5_header_path_spelling.ctu-infofile is missing, conflicts within the files read before it still reported: 1 pytest testtest_ctu_declaration_conflict_missing_ctu_infotest_ctu_macro_header_same_file_name_quote_and_angletest_ctu_macro_condition_evaluated_twicetest_ctu_macro_source_file_name_in_inactive_includetest_ctu_macro_header_included_by_macrotest_ctu_macro_header_included_in_other_casetest_ctu_macro_header_included_in_branch_hard_linktest_ctu_macro_header_included_in_branch_hard_link_in_subdirectorytest_ctu_macro_header_included_through_linktest_ctu_macro_header_included_from_parent_directorytest_addon_misra_2_5_header_relative_pathstest_addon_misra_2_5_header_relative_paths_defined_againUnchanged
static inlinefunction of a header reached through several paths#definewhose only use is in code that no checked configuration includes (-UX,--max-configs=1,#if 0)Details and examples
OPTS="-q --addon=misra --enable=style --template={file}:{line}:{id}"misra(--addon=misra, or<addon>misra</addon>in a project file). With only--addon=misra.pyor a JSON file, cppcheck does not report unmatchedmisra-*suppressions, Before PR and Nowlis a symlink toinc(ln -s inc l), shorter thanincdump: whatcppcheck --dumpwrites for a source file, one part per configuration, with the directives of the source file first, then those of its headerssummary: theMisraMacroline thatmisra.pyreports for one source file and one configuration, with the macros found there, cppcheck keeps it in a.ctu-infofilemisra.py, after every source file is analyzed, it reads all the.ctu-infofilesC3
Before PR, the declarations of a name were grouped all together for 2.5, by path string and line for the other rules.
The declarations of a name are looked up in a dict keyed by real path and line. Before PR a list per name was scanned for each new declaration (2.3, 2.4, 5.6, 5.7). This lookup is what makes S8 linear, not the real path.
C5
Also when a
.ctu-infofile is missing (for example a source file that cppcheck stopped on): the CTU step stops at it, as before. The 5.6 and 5.7 conflicts within the.ctu-infofiles read before it are reported, like 5.9, 8.5 and 8.6. 2.3, 2.4, 2.5, 5.8 and 8.7 are not reported.C7
#includehas, and a file that an active#includeof an active file reaches.#includereaches a file by its file name (case ignored on Windows). Among several files of the dump with directives and that name, it reaches:#include "...",#includeend alike (one ends with the other, the..components of the#includepath left out, the path of the dump can be the shorter one with-rp), compared by whole components (inc/myx/config.hdoes not end likex/config.h),#include MACRO).--includefollows the same rules.C8
Each summary (one per configuration of a source file, its headers included) contributes its last definition of the name. An
#undefthat the dump lists before every#defineof the name counts as a used definition. One finding if none of these last definitions is used, at the first path in the order of R13, then the smallest line.R1
Before PR, the unused
#defineis missed if the name is used in the other source file, or if its last directive there is an#undef(or an#undefcomes before any#define), in the order of the dump as in R2.Now it is reported, unless, in a configuration where this
#defineis active, its dump also lists an active#undefof the name after it, or before every active#defineof the name (counts as a use, as before).M 1is unused,M 2is used.cppcheck $OPTS a.c b.ca.c:1:misra-c2012-2.5R2
Neither definition is used. The dump lists the one of
h.hlast.cppcheck $OPTS a.ch.h:1:misra-c2012-2.5a.c:2:misra-c2012-2.5,h.h:1:misra-c2012-2.5R3
a.c, read first, reachesh.hthroughl.cppcheck $OPTS src/a.c src/b.cl/h.h:1:misra-c2012-2.5inc/h.h:1:misra-c2012-2.5Every path through a link: files of R3,
topis a symlink to the example directory (ln -s . top).top/inc/h.hgoes through one link,top/l/h.hthrough two. On Windows, starting cppcheck from a junction gives the same result.cppcheck $OPTS top/src/a.c top/src/b.ctop/l/h.h:1:misra-c2012-2.5, buttop/inc/h.h:1withtop/src/b.c top/src/a.ctop/inc/h.h:1:misra-c2012-2.5in both ordersR4
Files of R3, files in the other order.
cppcheck $OPTS src/b.c src/a.cinc/h.h:1:misra-c2012-2.5, butl/h.h:1in the order of R3inc/h.h:1:misra-c2012-2.5in both ordersR5
Files of R3, suppression with the path of
b.c.cppcheck $OPTS --suppress=misra-c2012-2.5:inc/h.h src/a.c src/b.cl/h.h:1:misra-c2012-2.5, but no output withsrc/b.c src/a.cR6
A path by which a source file reaches the header but that does not match the reported path, for example:
l/h.hwheninc/h.his reported../inc/h.hwhen an absolute path is reportedinc/h.hwhenreal/l/h.his reportedinc/h.hwhenW:\h.h(a Windowssubstdrive ofinc) is reportedINCLUD~1/h.hwheninclude_dir_long/h.his reported (INCLUD~1is the Windows 8.3 name ofinclude_dir_long)Link path: files of R3, suppression with the path of
a.c.cppcheck $OPTS --suppress=misra-c2012-2.5:l/h.h src/b.c src/a.cinc/h.h:1:misra-c2012-2.5, but no output withsrc/a.c src/b.cinc/h.h:1:misra-c2012-2.5in both orders..path against an absolute one: run fromsrc.a.creachesh.hthrough an absolute-I.<dir>is the absolute path of the example directory.cd src && cppcheck $OPTS -I"$(realpath ../inc)" --suppress=misra-c2012-2.5:../inc/h.h a.c b.c<dir>/inc/h.h:1:misra-c2012-2.5, but no output withb.c a.c<dir>/inc/h.h:1:misra-c2012-2.5in both ordersPath of the real directory, every path through a link:
real/lis a symlink toreal/inc(ln -s inc real/l),workspacea symlink toreal(ln -s real workspace).workspace/inc/h.handreal/l/h.hboth go through one link, the shorter one is reported.cppcheck $OPTS --suppress=misra-c2012-2.5:inc/h.h workspace/src/a.c real/src/b.creal/l/h.h:1:misra-c2012-2.5withreal/src/b.c workspace/src/a.creal/l/h.h:1:misra-c2012-2.5in both ordersR7
Files of R1.
cppcheck $OPTS --error-exitcode=1 a.c b.c; echo $?0a.c:1:misra-c2012-2.5, then1For R2 the Before PR finding must be suppressed, otherwise both give exit code 1. Files of R2.
cppcheck $OPTS --error-exitcode=1 --suppress=misra-c2012-2.5:h.h a.c; echo $?0a.c:2:misra-c2012-2.5, then1Mis used only in code that#if 0excludes (R10).cppcheck $OPTS --error-exitcode=1 a.c; echo $?a.c:2:misra-c2012-2.5, then10R8
The
#defineis unused in its source file and headers, and the dump lists the#undefafter another#defineof the name. For example a header#definecancelled in the source file that includes it, when that file also defines the name before.The dump lists the directives of
a.cbefore those ofh.h, so misra.py sees the second#undefbeforeM 1and countsM 1as unused. Without#define M 0/#undef M, misra.py counts the second#undefas a use and nothing is reported.cppcheck $OPTS --suppress=misra-c2012-20.5 a.c b.ch.h:1:misra-c2012-2.5witha.caloneh.h:1:misra-c2012-2.5With one source file, a
#definethat is not the last of its name in the dump is reported too, when an#undefof the source file that the dump lists before it cancels it.a.cincludesh.handk.h, each followed by an#undef M.cppcheck $OPTS --suppress=misra-c2012-20.5 a.ck.h:1:misra-c2012-2.5h.h:1:misra-c2012-2.5,k.h:1:misra-c2012-2.5R9
The path of every
#defineof the name is found from the current directory. Cases: hard link, bind mount, Windows\\?\or UNC path, relative paths that name other files from the current directory (with-rpormisra.pyrun directly).inc/h2.his a hard link toinc/h.h(ln inc/h.h inc/h2.h).Mis used throughh2.h, which is the same file ash.h, so the finding underinc/h.his a false positive.cppcheck $OPTS src/a.c src/b.cinc/h.h:1:misra-c2012-2.5R10
Inactive code: under
#if 0, in a branch excluded by-Dor-U, or in a header included only there.Still checked, in a header that C7 still takes for active:
#includefinds, when that header has no directive,#includedoes not find it next to the including file (<...>,-I, or no file at all, for example a system header or an include directory not given) and their paths end alike (y/config.hfor#include "config.h"found asx/config.hby-Ix,config.hfor#include <sub/config.h>found asinc/sub/config.hby-Iinc),#includeand there are several (the header it finds was first included through another path, for example#include "l/config.h"withla link toy, after#include "y/config.h"and#include "x/config.h"under#if 0: bothx/config.handy/config.hare then checked),#includenames its file through a macro.An
#undefthat follows only inactive#defines of the name (in a checked configuration) then comes before every#define(R1): it counts as a use of the active#defines that the dump lists after it, which Before PR it did not.Also not checked (false negatives): an active header that no active
#includereaches while an#includehas its file name, or whose file name is also the one of an#includethat reaches another header or is inactive. It happens when the header is reached through--include, through a hard link or a symbolic link to the file in another directory under another name, or when its path is not found from the current directory (R13).-DXexcludesM 2.M 1is unused (R2).cppcheck $OPTS -DX a.ca.c:4:misra-c2012-2.5a.c:2:misra-c2012-2.5h.his included only in inactive code.cppcheck $OPTS a.ch.h:1:misra-c2012-2.5Not checked although active (false negative):
--include=h.hmakesMactive, the dump has only the#includeunder#if 0. Files of the previous example.cppcheck $OPTS --include=h.h a.ch.h:1:misra-c2012-2.5--include, no output is expected)The
#undeffollows an inactive#defineonly.cppcheck $OPTS --suppress=misra-c2012-20.5 a.ca.c:5:misra-c2012-2.5#undef Mand#define M 2alone, Before PR already gave no output)R11
cppcheck $OPTS --suppress=misra-c2012-20.5 a.ca.c:1:misra-c2012-2.5R12
For example
#ifndef X, name used under#ifdef X.M 2is unused whenXis not defined,Mis used only when it is.cppcheck $OPTS a.ca.c:6:misra-c2012-2.5R13
For example with
-rp=prun from the parent ofp, withmisra.pyrun directly from another directory, or when a path has a non-ASCII character (hé.hishxx.hin the dump).Now, R1, R2, R8 and R12 do not apply to this name, R10 and R11 do. The finding is, of the last definitions of the name in each configuration of each source file, the one whose path comes first in the order of R3 with no link counted for a path not found (
l/h.hrather thaninc/h.h), then the smallest line.Files of R3 in a directory
p(p/lis the symlink), run from the parent directory ofp. The paths of the dump are relative top.cppcheck $OPTS -rp=p p/src/a.c p/src/b.cl/h.h:1:misra-c2012-2.5, butinc/h.h:1withp/src/b.c p/src/a.cl/h.h:1:misra-c2012-2.5in both ordersAn
#undefthat the dump lists before every#defineof the name counts as a definition here: with-rp=p,p/a.c=#undef Mandb.cdefiningMtwice give no 2.5, whereas without-rpthey giveb.c:1andb.c:2.cppcheck $OPTS --suppress=misra-c2012-20.5 -rp=p p/a.c b.c-rp=p, Now givesb.c:1:misra-c2012-2.5,b.c:2:misra-c2012-2.5)R14
Files of R3, commands of R5 and R6 with
--error-exitcode=1.cppcheck $OPTS --error-exitcode=1 --suppress=misra-c2012-2.5:inc/h.h src/a.c src/b.c; echo $?l/h.h:1:misra-c2012-2.5, then1, but0withsrc/b.c src/a.c0in both orderscppcheck $OPTS --error-exitcode=1 --suppress=misra-c2012-2.5:l/h.h src/b.c src/a.c; echo $?inc/h.h:1:misra-c2012-2.5, then1, but0withsrc/a.c src/b.cinc/h.h:1:misra-c2012-2.5, then1in both ordersR15
Only for a suppression without a line number: for example by id alone,
<id>:*, or with the name of a source file of the command line, a pattern too (for a header name or an inline suppression nothing is printed). Nothing is printed either when the suppression still matches another 2.5 finding.Files of the
#if 0example of R7.cppcheck $OPTS --enable=information --suppress=checkersReport --suppress=misra-c2012-2.5:a.c --error-exitcode=1 a.c; echo $?0a.c:0:unmatchedSuppression, then1S1
Several paths: link, absolute and relative, other case, 8.3 name or
substdrive on Windows.Tis used ina.c.cppcheck $OPTS src/a.c src/b.cinc/h.h:1:misra-c2012-5.6,l/h.h:1:misra-c2012-5.6,l/h.h:1:misra-c2012-2.3S2
Files of S1, with
void fa(void);instead ofextern T ta;, soTis unused. Without the suppression, Now reports onlyinc/h.h:1:misra-c2012-2.3.cppcheck $OPTS --suppress=*:inc/h.h src/a.c src/b.cl/h.h:1:misra-c2012-5.6,l/h.h:1:misra-c2012-2.3S3
cppcheck $OPTS a.c b.c c.ca.c:1:misra-c2012-5.6b.c:5:misra-c2012-8.5b.c:1:misra-c2012-5.6a.c:5:misra-c2012-8.5a.c:2:misra-c2012-5.7a.c:1:misra-c2012-5.6b.c:2:misra-c2012-5.7b.c:1:misra-c2012-5.6b.c:5:misra-c2012-8.5c.c:1:misra-c2012-5.6a.c:5:misra-c2012-8.5a.c:2:misra-c2012-5.7c.c:1:misra-c2012-5.6b.c:2:misra-c2012-5.7c.c:2:misra-c2012-5.7c.c:2:misra-c2012-5.7S4
The suppression is on a line that keeps tokens: a
structorenumdefinition, ortypedef struct {...} T;. Paths that do not match the reported one: those of R6 (link,..against an absolute path, Windows 8.3 name orsubstdrive).On a plain
typedef int T;line cppcheck only checks an inline suppression when a finding, even a suppressed one, is raised on that line. SounmatchedSuppressionappears under the reported path only, or not at all, and not under the other path, as with a single path. Before PR the false 5.6 or 2.3 under the other path made the line checked, so a suppression that is no longer needed could be reported then and be silent now (a plaintypedefline that was checked for another name), or the opposite (the main example below).Now,
unmatchedSuppressionalso for every path when the suppressed finding was a false positive of S1. The finding, if any, is suppressed through the reported path.The 5.7 of
h.his now reported only underinc/h.h, so the inline suppression read throughl/h.hmatches nothing.cppcheck $OPTS --enable=information --inline-suppr --suppress=checkersReport src/a.c src/b.c src/c.cinc/other.h:1:misra-c2012-5.7inc/other.h:1:misra-c2012-5.7,l/h.h:2:unmatchedSuppressionPlain
typedefline,Tunused.cppcheck $OPTS --enable=information --inline-suppr --suppress=checkersReport src/a.c src/b.cinc/h.h:2:misra-c2012-2.3,l/h.h:2:misra-c2012-2.3inc/h.h:2:misra-c2012-2.3,inc/h.h:2:unmatchedSuppressionS5
Files of S1. The two suppressions match the false 2.3 and 5.6 of Before PR, Now has no finding left for them.
cppcheck $OPTS --enable=information --suppress=checkersReport --suppress=misra-c2012-2.3 --suppress=misra-c2012-5.6 src/a.c src/b.cnofile:0:unmatchedSuppressionS6
Files of S4.
cppcheck $OPTS --enable=information --inline-suppr --suppress=checkersReport --suppress=misra-c2012-5.7:inc/other.h --error-exitcode=1 src/a.c src/b.c src/c.c; echo $?0l/h.h:2:unmatchedSuppression, then1S7
Files of S1.
cppcheck $OPTS --error-exitcode=1 src/a.c src/b.c; echo $?10S8
h.hholds 16000 linestypedef int T;. The second run findsa.cin the build dir and only runs the CTU step. Both sides report the same 2.3 and 5.6 findings, suppressed here.